package demo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NewJSON {


    public static void main(String[] args) {
        System.out.println("begin");

        oldSout();

        JSONArray jsonArray = new JSONArray();

        jsonArray.addAll(Arrays.asList(
                new MyJSON(1).put("t1",1).put("t2","t2"),
                new MyJSON(12).put("t3",33).put("t4","t4").put("t5",new JSONArray())
                ));

        System.out.println(JSON.toJSONString(jsonArray));

        System.out.println("end");
    }


    static class MyJSON extends JSONObject{

        public MyJSON (int size){
            super(size);
        }
        public MyJSON put(String k,Object v){
            super.put(k, v);
            return this;
        }

    }

    public static void oldSout(){

        JSONArray jsonArray = new JSONArray();

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("t1", 1);
        jsonObject1.put("t2", "t2");

        jsonArray.add(jsonObject1);

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("t3", 33);
        jsonObject2.put("t4", "t4");
        JSONArray jsonArray1 = new JSONArray();
        jsonObject2.put("t5", jsonArray1);

        jsonArray.add(jsonObject2);


        System.out.println(JSON.toJSONString(jsonArray));
    }

}
